"use strict";
cc._RF.push(module, 'a1032Ec7XtMVZwaKR5D4cnN', 'gameScene');
// script/cow_project/gameScene.js

"use strict";

cc.Class({
  "extends": cc.Component,
  properties: {
    cow_Prefab: {
      "default": null,
      type: cc.Prefab
    },
    rope_cow: {
      "default": [],
      type: cc.SpriteFrame
    },
    score_Num: {
      "default": null,
      type: cc.Label
    },
    gameOver: {
      "default": null,
      type: cc.Prefab
    }
  },
  // LIFE-CYCLE CALLBACKS:
  onLoad: function onLoad() {
    this.cowNode = cc.instantiate(this.cow_Prefab);
    this.node.addChild(this.cowNode);
    this.rope_node = this.node.getChildByName("rope");
    this.rope_node.active = false;
    this.cowNode.on(cc.Node.EventType.TOUCH_END, this.clickCapture, this);
    this.NumScore = 0;
    this.gameOverNode = cc.instantiate(this.gameOver);
    this.gameOverNode.active = false;
    this.node.addChild(this.gameOverNode);
  },
  start: function start() {
    var TimeDown = this.node.getChildByName('Time').getComponent(cc.Label);
    var time = 10;
    this.schedule(function () {
      time--;
      TimeDown.string = "Time:" + time + 's';

      if (time <= 0) {
        //游戏结束                
        var lastScore = this.gameOverNode.getChildByName("LastScore");
        lastScore.getComponent(cc.Label).string = "最终得分: " + this.NumScore;
        var level = this.gameOverNode.getChildByName("Level");

        switch (true) {
          case this.NumScore >= 0 && this.NumScore <= 2:
            level.getComponent(cc.Label).string = "套牛青铜";
            break;

          case this.NumScore >= 3 && this.NumScore <= 5:
            level.getComponent(cc.Label).string = "套牛星耀";
            break;

          case this.NumScore > 6:
            level.getComponent(cc.Label).string = "套牛王者";
            break;
        }

        this.gameOverNode.active = true;
        this.gameOverNode.setSiblingIndex(102);
        cc.director.pause();
        this.node.getChildByName("capture_button").getComponent(cc.Button).interactable = false;
        this.node.getChildByName("goStart").getComponent(cc.Button).interactable = false; // this.node.destroy();

        cc.log("游戏结束"); // cc.director.loadScene("gameOver");
      }
    }, 1);
  },
  update: function update(dt) {},
  clickCapture: function clickCapture(event, customEventData) {
    var _this = this;

    this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_cow[0];
    this.rope_node.active = true; //设置在父节点顺序

    this.rope_node.setSiblingIndex(100); //rope初始位置

    this.rope_node.position = cc.v2(0, -480); //rope向上移动

    var up = cc.moveTo(0.5, cc.v2(0, 60));
    var down = cc.moveTo(0.6, cc.v2(0, -480));
    var result = cc.callFunc(function () {
      var currentX = _this.cowNode.x;
      cc.log("currentX:", currentX);

      var number = _this.cowNode.getComponent("cow").number;

      if (currentX > -100 && currentX < 100) {
        cc.log("牛被抓到");

        _this.node.removeChild(_this.cowNode);

        _this.rope_node.getComponent(cc.Sprite).spriteFrame = _this.rope_cow[number + 1];
        _this.cowNode = cc.instantiate(_this.cow_Prefab);

        _this.node.addChild(_this.cowNode); //分数


        _this.NumScore += 1;

        var scoreLabel = _this.node.getChildByName('ScoreNumber').getComponent(cc.Label);

        scoreLabel.string = "Score: " + _this.NumScore;
      } else {
        cc.log("牛没被抓到");
        _this.rope_node.getComponent(cc.Sprite).spriteFrame = _this.rope_cow[0];
      }
    }, this);
    var seq = cc.sequence(up, result, down);
    this.rope_node.runAction(seq);
  },
  click_goStart: function click_goStart(event, customEventData) {
    cc.director.loadScene("Frontgame");
    this.node.destroy();
  }
});

cc._RF.pop();